var express = require('express');
var router = express.Router();
var logger = require('../common/logger').logger.getLogger("user");
var mysql_query = require('../common/mysql');
var httpresult = require('../common/response');
var md5 = require('md5');
var vertoken = require('../common/token');
var define = require('../common/define');
var bank_model = require('../models/bank');

router.post('/add', async function(req, res, next) {
    var [err, data] = await mysql_query("insert into bank (name, provider, chapter_id, type, course_id) value (?, ?, ?, ?, ?)",
        [req.body.name, req.user_id, req.body.chapter_id || 0, req.body.type, req.body.course_id]);;
    if (err) {
        logger.warn(err);
        return res.send(httpresult.sys_error(err));
    }
    if (data.length === 0) {
        return res.send(httpresult.trans_error("COURSE_NOT_EXIST", "该科目不存在"));
    }
    var result = {
        info: data[0]
    }
    return res.send(httpresult.success(result));
});

router.post('/update', async function(req, res, next) {
    var [err, data] = await mysql_query(
        "update bank set name = ?, chapter_id = ?, provider = ?, type = ?, course_id = ? where bank_id = ?",
        [req.body.name, req.body.chapter_id || 0, req.user_id, req.body.type, req.body.course_id || 0, req.body.bank_id]);;
    if (err) {
        logger.warn(err);
        return res.send(httpresult.sys_error(err));
    }
    if (data.length === 0) {
        return res.send(httpresult.trans_error("BANK_NOT_EXIST", "该试卷不存在"));
    }
    var result = {
        info: data[0]
    }
    return res.send(httpresult.success(result));
});

router.post('/delete', async function(req, res, next) {
    let [err, data] = await bank_model.del(req.body.bank_id);
    if (err) {
        logger.warn(err);
        return res.send(httpresult.sys_error(err));
    }
    return res.send(httpresult.success(data));
});
router.post('/info', async function(req, res, next) {
    var [error, data] = await mysql_query("select bank_id, name, provider, chapter_id, type, course_id from bank where bank_id = ?", [req.body.bank_id]);
    if (error) {
        logger.warn(error);
        return res.send(httpresult.sys_error(error));
    }
    if (data.length === 0) {
        return res.send(httpresult.trans_error("COURSE_NOT_EXIST", "该试卷不存在"));
    }
    var result = data[0]
    return res.send(httpresult.success(result));
});

router.post('/list', async function(req, res, next) {
    var error, data;
    var sql = "select bank_id, bank.name as name, provider, bank.chapter_id as chapter_id, " +
        "type, bank.course_id, introduce, chapter.name as chapter_name from bank " +
        "left join chapter on bank.chapter_id = chapter.chapter_id where bank.course_id = ? ";
    var where = [req.body.course_id];
    if (req.body.chapter_ids && req.body.chapter_ids.length > 0) {
        sql += " and chapter.chapter_id in (?) ";
        where.push(req.body.chapter_ids);
    }
    if (req.body.type) {
        sql += " and type = ? ";
        where.push(req.body.type);
    }

    if (req.body.provider) {
        sql += " and bank.provider = ?";
        where .push(req.body.provider);
    }
    [error, data] = await  mysql_query(sql, where);
    if (error) {
        logger.warn(error);
        return res.send(httpresult.sys_error(error));
    }

    for (let index in data) {
        data[index].type_name = define.bank_type_info[data[index].type];
    }

    data = data.sort((a, b) => {
        return b.bank_id - a.bank_id;
    });
    return res.send(httpresult.success(data));
});

module.exports = router;
